package Amazon::Bezos;
use strict;
use warnings;

use Amazon::Bezos::Account;

our $VERSION = '0.001';

sub new {
    my $class = shift;
    return bless {acct => Amazon::Bezos::Account->new(@_)}, $class;
}

sub account { return $_[0]->{acct}; }

sub s3  { return $_[0]->{s3}  || $_[0]->init_s3; }
sub sqs { return $_[0]->{sqs} || $_[0]->init_sqs; }
sub ec2 { return $_[0]->{ec2} || $_[0]->init_ec2; }

sub buckets {
    my $self = shift;
    my $s3 = $self->{s3} || $self->init_s3;
    return $s3->buckets;
}

sub queues {
    my $self = shift;
    my $sqs = $self->{sqs} || $self->init_sqs;
    return $sqs->ListQueues(@_);
}

sub images {
    my $self = shift;
    my $ec2 = $self->{ec2} || $self->init_ec2;
    return $ec2->describe_images(@_);
}

sub instances {
    my $self = shift;
    my $ec2 = $self->{ec2} || $self->init_ec2;
    return $ec2->describe_instances(@_);
}

# sub domains { }

#--- initializers

sub init_s3 {
    my $self = shift;
    my $acct = $self->{acct};
    require Amazon::S3;
    my $s3 =
      Amazon::S3->new(
                      {
                       aws_access_key_id     => $acct->access_key_id,
                       aws_secret_access_key => $acct->secret_access_key,
                       retry                 => 1,
                      }
      );
    $self->{s3} = $s3;
    return $s3;
}

sub init_sqs {
    my $self = shift;
    my $acct = $self->{acct};
    require Amazon::SQS::Simple;
    my $sqs =
      Amazon::SQS::Simple->new($acct->access_key_id, $acct->secret_access_key);
    $self->{sqs} = $sqs;
    return $sqs;
}

sub init_ec2 {
    my $self = shift;
    my $acct = $self->{acct};
    require Net::Amazon::EC2;
    my $ec2 =
      Net::Amazon::EC2->new(AWSAccessKeyId  => $acct->access_key_id,
                            SecretAccessKey => $acct->secret_access_key,);
    $self->{ec2} = $ec2;
    return $ec2;
}

1;

__END__

=head1 NAME

Amazon::Bezos - Bezos is a Perl toolkit for working with the
Amazon Web Services cloud in an integrated and seamless
manner.

=head1 SYNOPSIS

  #!/usr/bin/perl
  use warnings;
  use strict;

  use Amazon::Bezos;
  
  # create account using env variables. see TESTING.
  my $bezos = Amazon::Bezos->new; 

  # read in Test account from /your/home/bezos.yaml
  # $bezos = Amazon::Bezos->('Test'); 

  # read in Foo account info from /path/to/other/file.yaml
  # $bezos = Amazon::Bezos->('Foo', '/path/to/other/file.yaml');

  my $acct = $bezos->account;  
    
  my $s3 = $bezos->s3;   # returns initialized Amazon::S3 object
  my $sqs = $bezos->sqs; # returns initialized Amazon::SQS::Simple object
  my $ec2 = $bezos->ec2; # returns initialized Net::Amazon::EC2 object
  
=head1 DESCRIPTION

Amazon::Bezos - Bezos is a Perl package for working with
Amazon Web Services cloud in a (loosely) integrated and seamless
manner.

B<THIS PROJECT IS IN ITS VERY EARLY STAGES OF DEVELOPMENT.>

=head2 Objectives

=over

=item Easy to install

=item Foundation for more specific systems utilizing the AWS cloud

=item Command line shell tools for working with services

=back

=head2 Milestones

=head3 M1

=over
 
=item Shared and multiple account information across service clients

=back

=head3 M2

=over

=item Command tool(s) for working with cloud services using L<App::Cmd>

=back

=head3 M3

=over

=item L<Amazon::SimpleDB> support.

=back

=head3 M4

=over

=item Consistent uniform interface accross all service interfaces

=item Uniform error handling

=item Reduce number of dependencies

=back

=head3 Future Considerations

=over

=item complex and compound functions

=item shared utility functions

=item B<You tell me!>

=back

To sign up for an Amazon Web Services account, required to
use this library and the S3 service, please visit the Amazon
Web Services web site at http://www.amazonaws.com/.

You will be billed accordingly by Amazon when you use this
module and must be responsible for these costs.

=head1 METHODS

=head2 new 

Create a new S3 client object. Takes some arguments:

=over

=item aws_access_key_id 

Use your Access Key ID as the value of the AWSAccessKeyId parameter
in requests you send to Amazon Web Services (when required). Your
Access Key ID identifies you as the party responsible for the
request.

=item aws_secret_access_key 

Since your Access Key ID is not encrypted in requests to AWS, it
could be discovered and used by anyone. Services that are not free
require you to provide additional information, a request signature,
to verify that a request containing your unique Access Key ID could
only have come from you.

B<DO NOT INCLUDE THIS IN SCRIPTS OR APPLICATIONS YOU
DISTRIBUTE. YOU'LL BE SORRY.>

=back

=head2 account

=head2 s3

=head2 sqs

=head2 ec2

=head2 buckets

=head2 queues

=head2 instances

=head2 images

=head2 init_ec2

=head2 init_s3

=head2 init_sqs

=head1 PROJECT SITE

A Google Code project site has been setup. It can be accessed here:

http://code.google.com/p/bezos/

=head1 TESTING

Testing S3 is a tricky thing. Amazon wants to charge you a bit of 
money each time you use their service. And yes, testing counts as using.
Because of this, the application's test suite skips anything approaching 
a real test unless you set these three environment variables:

=over 

=item AMAZON_S3_EXPENSIVE_TESTS

Doesn't matter what you set it to. Just has to be set

=item AWS_ACCESS_KEY_ID 

Your AWS access key

=item AWS_ACCESS_KEY_SECRET

Your AWS sekkr1t passkey. Be forewarned that setting this environment variable
on a shared system might leak that information to another user. Be careful.

=back

=head1 TO DO

=over

=item Continued to improve and refine of documentation.

=item Reduce dependencies wherever possible.

=item Implement debugging mode.

=item See L<MILESTONES>

=back

=head1 SUPPORT

Bugs should be reported via the Google Code project bug tracker

<http://code.google.com/p/bezos/issues/list>

For other issues, contact the author.

=head1 AUTHOR

Timothy Appnel <tima@cpan.org>

=head1 COPYRIGHT AND LICENCE

The software is released under the Artistic License. The
terms of the Artistic License are described at
http://www.perl.com/language/misc/Artistic.html. Except
where otherwise noted, Amazon::Bezos is Copyright 2008, Timothy
Appnel, tima@cpan.org. All rights reserved.

